import { open } from '@tauri-apps/plugin-dialog';
import { call } from './tauri-api.js'

// 打开文件选择窗口
const openFileDialog = async function () {
  try {
    const file = await open({
      multiple: false,
      directory: false,
    });
    return file;
  } catch (error) {
    console.error('打开文件对话框失败:', error);
    throw error;
  }
}

// 获取文件内容
const getFileContent = async function (filePath) {
  try {
    return await call("get_file_content", {filePath: filePath});
  } catch (error) {
    console.error('获取文件内容失败:', error);
    throw error;
  }
}

// 获取所有规则列表
const getAllRules = async function (ruleContent) {
  try {
    return await call("get_all_rules", {grammarText: ruleContent});
  } catch (error) {
    console.error('获取规则列表失败:', error);
    throw error;
  }
}

// 运行规则
const runGrammar = async function (grammarText, ruleName, inputText) {
  try {
    return await call("run_grammar", {
      grammarText: grammarText,
      ruleName: ruleName,
      inputText: inputText
    });
  } catch (error) {
    console.error('运行语法规则失败:', error);
    throw error;
  }
}

// 格式化
const formatGrammar = async function (grammar) {
  try {
    return await call("format_grammar", { grammar: grammar });
  } catch (error) {
    console.error('格式化语法失败:', error);
    throw error;
  }
}


export {
  openFileDialog,
  getFileContent,
  getAllRules,
  runGrammar,
  formatGrammar
}